﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Extentions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using TrainingReminderSystem.Bll;
using TrainingReminderSystem.Model;

namespace TrainingReminderSystem.Filters
{
    /// <summary>
    /// 报错记录日志用的
    /// </summary>
    public class LogExceptionAttribute : IExceptionFilter
    {
        /// <summary>
        /// 多线程不会触发此
        /// </summary>
        /// <param name="context"></param>
        public void OnException(ExceptionContext context)
        {
            //string controller = context.RouteData.Values["Controller"].ToString();//获取Action
            //string action = context.RouteData.Values["Action"].ToString();//获取请求方法
            //string method = context.HttpContext.Request.Method;//获取请求头部
            //var headrs = context.HttpContext.Request.Headers;//获取请求参数
            //var queryString = context.HttpContext.Request.QueryString;
            //var forms = context.HttpContext.Request.Form;
            var ar = new AjaxResult<int>() { Success = false, Total = (int)EnumTotal.异常 };
            if (context.Exception is ExException)
            {
                context.ExceptionHandled = true;
                var msg2 = context.Exception.ExMessageWithInner();
                ar.Message = LanguageHelper.GetInstance().Fail + ":" + msg2;
                var jr2 = new JsonResult(ar);
                context.Result = jr2;
                return;
            }
            if (context.Exception is Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException)
            {
                context.ExceptionHandled = true;
                ar.Message = LanguageHelper.GetInstance().ConcurrencyException;
                var jr2 = new JsonResult(ar);
                context.Result = jr2;
                return;
            }
            string other = PcLoginedBll.GetCurrentOther();
            context.Exception.ExLogError(other);
            context.ExceptionHandled = true;
            var msg = context.Exception.ExMessageWithInner();
            ar.Message = LanguageHelper.GetInstance().Fail + ":" + msg;
            var jr = new JsonResult(ar);
            context.Result = jr;
        }
    }
}
